﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 6/27/2009
 * Time: 11:21 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Text;

namespace popLib.ExtensionClass
{
	/// <summary>
	/// Description of MCIPlayer.
	/// </summary>
	public class MCIPlayer
	{
		public enum PlayerStatus { Ready, Playing, Paused, Stopped };		
		string alias;
		
		public MCIPlayer(string alias)
		{
			this.alias = alias;
		}
		
		public void Open(string filePath)
		{
			string sCommand = "open \"" + filePath +"\" type mpegvideo alias "+alias;
			APILib.mciSendString(sCommand, null, 0, IntPtr.Zero);
		}
		
		public void Play(){
			APILib.mciSendString("play "+alias, null, 0, IntPtr.Zero);
		}
		
		public void Pause()
		{
			APILib.mciSendString("pause "+alias, null, 0, IntPtr.Zero);
		}
		
		public void Stop()
		{
			try{
				APILib.mciSendString("stop "+alias, null, 0, IntPtr.Zero); 
			}catch{}
		}
		
		public void Seek(long value)
		{
			value = value * 1000;
				APILib.mciSendString("seek "+alias+" to " + value.ToString(), null, 0, IntPtr.Zero);
			Play();
		}
		
		public void Close()
		{
			try{
				APILib.mciSendString("close "+alias, null, 0, IntPtr.Zero);
			}catch {}
		}
		
		public void DisplayMediaWindow()
		{
			APILib.mciSendString("put "+alias, null, 0, IntPtr.Zero);
		}

		public void DisplayMediaWindow(Rectangle windowInfo)
		{
			APILib.mciSendString( string.Format("put {0} window at {1} {2} {3} {4}", alias, windowInfo.Left, windowInfo.Top, windowInfo.Width, windowInfo.Height), null, 0, IntPtr.Zero);
		}
		
		public string Status{
			get{
				int length = 128;
				StringBuilder strBuffer = new StringBuilder(length);
				APILib.mciSendString("status YinYangMedia mode", strBuffer, length, IntPtr.Zero);
				return strBuffer.ToString();
			}
		}
		
		public string Alias{
			get{ return alias;}
		}
		
		public long Length{
			get{
				StringBuilder fileleng = new StringBuilder(256);
				APILib.mciSendString("status YinYangMedia length", fileleng, 256, IntPtr.Zero);
				try{
					return (long)Math.Floor(Convert.ToDouble(fileleng.ToString().Trim()) / 1000);
				}catch { return 0; }
			}
		}
		
		public long Current{
			get{
				StringBuilder fileleng = new StringBuilder(256);
				APILib.mciSendString("status YinYangMedia position", fileleng, 256, IntPtr.Zero);
				try{
					return (long)Math.Floor(Convert.ToDouble(fileleng.ToString().Trim()) / 1000);
				}catch { return 0; }
			}
		}
		
		private string FormatSeconds(long seconds){
			long minus = 0;
			if (seconds < 3600){
				minus = seconds / 60;
				seconds = seconds % 60;
				return minus.ToString().PadLeft(2, '0') + ":" + seconds.ToString().PadLeft(2, '0');
			}else{				
				long hours = seconds / 3600;
				minus = seconds / 60;
				seconds = seconds % 60;
				return hours.ToString().PadLeft(2, '0') + ":" + minus.ToString().PadLeft(2, '0') + ":" + seconds.ToString().PadLeft(2, '0');
			}
		}
	}
}
